# coding: utf-8

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.wait import WebDriverWait
import unittest, time, re

class Untitled(unittest.TestCase):
    main_page = "/php4dvd/#!/sort/name%20asc/"

    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "http://localhost"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_login(self):
        driver = self.driver
        # open | /php4dvd/ |
        driver.get(self.base_url + "/php4dvd/")
        # type | id=username | admin
        username = driver.find_element_by_id("username")
        username.clear()
        username.send_keys("admin")
        # type | name=password | admin
        password = driver.find_element_by_name("password")
        password.clear()
        password.send_keys("admin")
        # click | name=submit |
        driver.find_element_by_name("submit").click()

        # make sure we are not at login page
        self.assertNotEqual(self.driver.current_url , self.base_url + "/php4dvd/")

    def test_create_film(self):
        self.test_login()
        driver = self.driver
        # open | /php4dvd/#!/sort/name%20asc/ |
        driver.get(self.base_url + self.main_page)
        # click | css=img[alt="Add movie"] |
        driver.find_element_by_css_selector("img[alt=\"Add movie\"]").click()
        # type | name=name | Somefilm
        name = driver.find_element_by_name("name")
        name.clear()
        name.send_keys("Somefilm")
        # type | name=year | 2014
        year = driver.find_element_by_name("year")
        year.clear()
        year.send_keys("2014")
        # type | name=notes | very nice movie. Would like to see it again.
        notes = driver.find_element_by_name("notes")
        notes.clear()
        notes.send_keys("Very nice movie. Would like to see it again.")
        # type | id=text_languages_0 | english, russian
        text_languages = driver.find_element_by_id("text_languages_0")
        text_languages.clear()
        text_languages.send_keys("english, russian")
        # type | name=subtitles | russian
        subtitles = driver.find_element_by_name("subtitles")
        subtitles.clear()
        subtitles.send_keys("russian")
        # click | id=submit |
        driver.find_element_by_id("submit").click()

        # need to redirect to another page
        self.assertEqual(self.driver.current_url.split('&')[0] , self.base_url + "/php4dvd/?go=movie")

    def test_create_film_without_required_fields(self):
        self.test_login()
        driver = self.driver
        # open | /php4dvd/#!/sort/name%20asc/ |
        driver.get(self.base_url + self.main_page)
        # click | css=img[alt="Add movie"] |
        driver.find_element_by_css_selector("img[alt=\"Add movie\"]").click()
        # type | name=name | Somefilm
        name = driver.find_element_by_name("name")
        name.clear()
        name.send_keys("Somefilm")
        # click | id=submit |
        driver.find_element_by_id("submit").click()

        # looking for error msg
        self.assertTrue(self.is_element_present(By.CLASS_NAME, 'error'))
        # need to stay on same page
        self.assertEqual(self.driver.current_url , self.base_url + "/php4dvd/?go=add")

    def test_delete_film(self):
        self.test_login()
        driver = self.driver
        # open | /php4dvd/#!/sort/name%20asc/ |
        driver.get(self.base_url + self.main_page)
        # click | css=div.nocover |
        driver.find_element_by_css_selector("div.nocover").click()
        # click | css=img[alt="Remove"] |
        driver.find_element_by_css_selector("img[alt=\"Remove\"]").click()
        # assertConfirmation | Are you sure you want to remove this? |
        self.assertRegexpMatches(self.close_alert_and_get_its_text(), r"^Are you sure you want to remove this[\s\S]$")

        # need to redirect to another page
        self.assertEqual(self.driver.current_url , self.base_url + self.main_page)

    def test_find_existing_film(self):
        self.test_login()
        driver = self.driver
        # open | /php4dvd/#!/sort/name%20asc/ |
        driver.get(self.base_url + self.main_page)
        # type | id=q | somefilm
        search = driver.find_element_by_id("q")
        search.clear()
        search.send_keys("somefilm")
        search.send_keys(Keys.ENTER)

        # looking for some links to film
        self.assertTrue(self.is_element_present(By.XPATH, '//*[@id="results"]/a[1]'))

    def test_find_non_existing_film(self):
        self.test_login()
        driver = self.driver
        # open | /php4dvd/#!/sort/name%20asc/ |
        driver.get(self.base_url + self.main_page)
        # type | id=q | another
        search = driver.find_element_by_id("q")
        search.clear()
        search.send_keys("anotherfilm")
        search.send_keys(Keys.ENTER)

        # looking there is div inside #id=result with .content (No movies where found.)
        self.assertTrue(self.is_element_present(By.XPATH, '//*[@id="results"]/div[@class="content"]'))

    # сначало думал что нужно удалять часть описания
    #def test_delete_description(self):
    #    self.test_login()
    #    driver = self.driver
    #    # open | /php4dvd/#!/sort/name%20asc/ |
    #    driver.get(self.base_url + "/php4dvd/#!/sort/name%20asc/")
    #    # click | css=div.nocover |
    #    driver.find_element_by_css_selector("div.nocover").click()
    #    # click | css=img[alt="Edit"] |
    #    driver.find_element_by_css_selector("img[alt=\"Edit\"]").click()
    #    # type | name=notes |
    #    driver.find_element_by_name("notes").clear()
    #    driver.find_element_by_name("notes").send_keys("")
    #    # type | id=text_languages_0 |
    #    driver.find_element_by_id("text_languages_0").clear()
    #    driver.find_element_by_id("text_languages_0").send_keys("")
    #    # type | name=subtitles |
    #    driver.find_element_by_name("subtitles").clear()
    #    driver.find_element_by_name("subtitles").send_keys("")
    #    # click | id=submit |
    #    driver.find_element_by_id("submit").click()
    #
    #    # need to redirect to ?go=movie&... page
    #    self.assertEqual(self.driver.current_url.split('&')[0] , self.base_url + "/php4dvd/?go=movie")

    #def test_delete_description_with_required_fields(self):
    #    self.test_login()
    #    driver = self.driver
    #    # open | /php4dvd/#!/sort/name%20asc/ |
    #    driver.get(self.base_url + "/php4dvd/#!/sort/name%20asc/")
    #    # click | css=div.nocover |
    #    driver.find_element_by_css_selector("div.nocover").click()
    #    # click | css=img[alt="Edit"] |
    #    driver.find_element_by_css_selector("img[alt=\"Edit\"]").click()
    #    # type | name=year |
    #    driver.find_element_by_name("year").clear()
    #    driver.find_element_by_name("year").send_keys("")
    #    # type | name=notes |
    #    driver.find_element_by_name("notes").clear()
    #    driver.find_element_by_name("notes").send_keys("")
    #    # type | id=text_languages_0 |
    #    driver.find_element_by_id("text_languages_0").clear()
    #    driver.find_element_by_id("text_languages_0").send_keys("")
    #    # type | name=subtitles |
    #    driver.find_element_by_name("subtitles").clear()
    #    driver.find_element_by_name("subtitles").send_keys("")
    #    # click | id=submit |
    #    driver.find_element_by_id("submit").click()
    #
    #    # need to stay on same page after submiting
    #    self.assertEqual(self.driver.current_url.split('&')[0] , self.base_url + "/php4dvd/?go=edit")

    def is_element_present(self, how, what):
        try:
            self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e:
            return False
        return True

    def is_alert_present(self):
        try:
            self.driver.switch_to_alert()
        except NoAlertPresentException, e:
            return False
        return True

    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()
